

package top.continew.admin.question.manger.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import top.continew.admin.question.manger.service.model.QueryPoint;
import top.continew.admin.question.mapper.question.QuestionKnowledgePointMapper;
import top.continew.admin.question.model.question.Question;
import top.continew.admin.question.model.question.QuestionKnowledgePoint;
import top.continew.admin.question.manger.service.PointService;
import top.continew.admin.question.manger.service.model.PointVO;
import top.continew.admin.question.manger.service.model.Value;
import top.continew.starter.core.validation.ValidationUtils;

import java.util.List;

@Service
public class PointServiceImpl implements PointService {

    private QuestionKnowledgePointMapper pointMapper;

    public PointServiceImpl(QuestionKnowledgePointMapper pointMapper) {
        this.pointMapper = pointMapper;
    }

    @Override
    public List<Value> getPointList(QueryPoint queryPoint) {
        return pointMapper.getPointList(queryPoint);
    }

    @Override
    public List<PointVO> getQuestionPointList(Long questionId) {
        return pointMapper.getQuestionPoint(questionId);
    }

    @Override
    public void addQuestionCorrelationPoint(Long questionId, Long pointId, String name) {
        Question question = new Question().selectById(questionId);
        ValidationUtils.throwIfEmpty(question, "题目不存在");
        QuestionKnowledgePoint questionPoint = new QuestionKnowledgePoint();
        questionPoint.setKnowledgeId(pointId);
        questionPoint.setName(name);
        questionPoint.setQuestionId(questionId);
        questionPoint.setSort(System.currentTimeMillis() * 1000);
        questionPoint.setCtime(System.currentTimeMillis());
        questionPoint.setUtime(System.currentTimeMillis());
        questionPoint.insertOrUpdate();
    }

    @Override
    public void deleteQuestionCorrelationPoint(String questionId, String pointId) {
        QuestionKnowledgePoint questionPoint = new QuestionKnowledgePoint()
            .selectOne(new QueryWrapper<QuestionKnowledgePoint>().eq("question_id", questionId)
                .eq("knowledge_id", pointId));
        ValidationUtils.throwIfEmpty(questionPoint, "数据不存在");
        questionPoint.deleteById();
    }

    @Override
    public void pointSort(String questionId, String pointId, Long sort) {
        QuestionKnowledgePoint questionPoint = new QuestionKnowledgePoint()
            .selectOne(new QueryWrapper<QuestionKnowledgePoint>().eq("question_id", questionId)
                .eq("knowledge_id", pointId));
        if (questionPoint == null) {
            return;
        }
        questionPoint.setSort(sort);
        questionPoint.updateById();
    }
}
